#include "newcputools.h"
#include "ui_newcputools.h"
#include "cpu_tool_qt.h"
#include <QtCore>
#include <QDebug>
#include <QWidget>
#include <QComboBox>
#include <QTime>
#include <QFileDialog>
#include <QMessageBox>
#include <QTimer>
#include <QProcess>

NewCpuTools::NewCpuTools(QWidget *parent) :
	QMainWindow(parent),
	ui(new Ui::NewCpuTools)
{
	ui->setupUi(this);
	setCpuInfoAvailable();
}

NewCpuTools::~NewCpuTools()
{
	delete ui;
}

//初始化界面控件中的信息
void NewCpuTools::setCpuInfoAvailable()
{
	int i = 0;
	//set cpu num comboBox
	for(i = 0;i < get_cpu_num();i++)
	{
		ui->comboBox_CpuNum->insertItem(i,QString::number(i));
		ui->comboBox_CpuNumW->insertItem(i,QString::number(i));
	}

	//set cpu info comboBox
	ui->comboBox_CpuInfo->addItem("Current Freq");
	ui->comboBox_CpuInfo->addItem("Max Freq");
	ui->comboBox_CpuInfo->addItem("Min Freq");
	ui->comboBox_CpuInfo->addItem("Available Freq");
	ui->comboBox_CpuInfo->addItem("Available Governor");
	ui->comboBox_CpuInfo->addItem("Current Governor");

	ui->comboBox_CpuInfoW->addItem("Set Governor");
	ui->comboBox_CpuInfoW->addItem("Set Freq");

	//set store data comboBox
	ui->comboBox_DataToStore->addItem("CPU");
	ui->comboBox_DataToStore->addItem("NetWork");
	ui->comboBox_DataToStore->addItem("Memory");
	ui->comboBox_DataToStore->addItem("Disk");

	//set gen comboBox
	ui->comboBox_GenOutputFileType->addItem("jpg");
	ui->comboBox_GenOutputFileType->addItem("pdf");
	//ui->comboBox_GenOutputFileType->addItem("png");
	//ui->comboBox_GenOutputFileType->addItem("csv");
}

//GET按钮按下的响应函数
void NewCpuTools::on_pushButtonGetCpuInfo_clicked()
{
	//获取当前许可查看信息的索引号
	int currentCmd = ui->comboBox_CpuInfo->currentIndex();
	//获取当前许可查看信息的文本
	QString currentQString = ui->comboBox_CpuInfo->currentText();
	//获取所以查看的cpu
	int currentCpuNum = ui->comboBox_CpuNum->currentIndex();
	QString result1 ;
	//在执行结果这显示添加当前时间显示
	result1 += QDate::currentDate().toString("yyyy-MM-dd") + " " + QTime::currentTime().toString("hh:mm:ss") + "\n";
	result1 += currentQString + ":" ;

	QString result2;
	char cResult[1024];
	switch(currentCmd)
	{
	case 0:
		//获取当前cpu频率
		result1 += QString::number(get_scaling_cur_freq(currentCpuNum));
		break;
	case 1:
		//获取当前cpu的最大频率
		result1 += QString::number(get_scaling_max_freq(currentCpuNum));
		break;
	case 2:
		//获取当前cpu的最小频率
		result1 += QString::number(get_scaling_min_freq(currentCpuNum));
		break;
	case 3:
		//获取当前可用频率
		get_scaling_available_frequences(cResult, currentCpuNum);
		result2 = QString::fromAscii(cResult, strlen(cResult));
		break;
	case 4:
		//获取当前可用cpu调度器
		get_scaling_available_governors(cResult, currentCpuNum);
		result2 = QString::fromAscii(cResult, strlen(cResult));
		break;
	case 5:
		//获取当前cpu调度器
		get_scaling_governor(cResult, currentCpuNum);
		result2 = QString::fromAscii(cResult, strlen(cResult));
		break;
	}
	//将执行结果追加到文本框中
	ui->textEdit->append(result1+result2 + "\n");
}

//SET按钮按下的响应函数
void NewCpuTools::on_pushButtonSetCpuInfo_clicked()
{
	//获取需要设置的选项索引号
	int currentCmd = ui->comboBox_CpuInfoW->currentIndex();
	//获取需要设置的选项的文本格式
	QString currentQString = ui->comboBox_CpuInfoW->currentText();
	//获取即将设置的cpu
	int currentCpuNum = ui->comboBox_CpuNumW->currentIndex();

	//获取设置的参数
	QString currentParam = ui->comboBox_Param->currentText();
	QString result1 ;
	char cResult[1024];
	//在执行结果这显示添加当前时间显示
	result1 += QDate::currentDate().toString("yyyy-MM-dd") + " " +
		QTime::currentTime().toString("hh:mm:ss") + "\n";
	result1 += currentQString + ":" ;
	switch(currentCmd)
	{
	case 0:
		//设置cpu调度器
		set_cpu_governor(currentParam.toAscii().constData(),currentCpuNum);
		//获取当前cpu调度器
		get_scaling_governor(cResult, currentCpuNum);
		result1 += QString::fromAscii(cResult, strlen(cResult));
		break;
	case 1:
		//设置cpu频率
		set_scaling_setspeed(currentParam.toInt(),currentCpuNum);
		//获取当前cpu频率
		result1 += QString::number(get_scaling_cur_freq(currentCpuNum));
		break;
	}
	//将执行结果追加到文本框中
	ui->textEdit_W->append(result1);
}

//SET 前的选择框cpuinfo的内容改变后的响应函数
void NewCpuTools::on_comboBox_CpuInfoW_currentIndexChanged(int index)
{
	char cResult[1024];
	QString result;
	int i = 0;
	//清除设置参数选择框的内容
	ui->comboBox_Param->clear();
	switch(index)
	{
	case 0:
		//获取可用的cpu调度器
		get_scaling_available_governors(cResult, 0);
		//将char × 格式字符串转换为QString
		result = QString::fromAscii(cResult, strlen(cResult));
		//分离结果中的字符串，解析到参数选择框中
		for(i = 0; i < result.count(' ');i++)
		{
			ui->comboBox_Param->addItem(result.section(' ',i,i));
		}
		break;
	case 1:
		//获取可用的cpu频率
		get_scaling_available_frequences(cResult, 0);
		//将char × 格式字符串转换为QString
		result = QString::fromAscii(cResult, strlen(cResult));
		//分离结果中的字符串，解析到参数选择框中
		for(i = 0; i < result.count(' ');i++)
		{
			ui->comboBox_Param->addItem(result.section(' ',i,i));
		}
		break;
	}
}

//Reset Output Select按钮的响应函数
void NewCpuTools::on_pushButton_clicked()
{
	//使能文件格式选择控件
	ui->comboBox_GenOutputFileType->setDisabled(false);
	//清除文件名
	ui->lineEdit_GenInputFileName->clear();
	ui->label_GenSelectedOutputFileName->clear();
}

//Start To Store Data按钮按下的响应函数
void NewCpuTools::on_pushButton_StartStoreData_clicked()
{
	if(ui->lineEdit_StoreFIleName->text().isEmpty())
	{
		QMessageBox::warning(this,"Warning","Error! No File Name!");
	}
	else{
		QString file;
		QFileDialog qfd;
		ui->pushButton_StartStoreData->setDisabled(true);
		file = qfd.getExistingDirectory(this,"Whers to Save",".", QFileDialog::ShowDirsOnly);

		if(file.isEmpty())
		{
			QMessageBox::warning(this,"Warning","No Dir Selected!");
		}
		else{
			file += "/" + ui->comboBox_DataToStore->currentText() + "-" +
				ui->lineEdit_StoreFIleName->text() + ".txt";
			switch(ui->comboBox_DataToStore->currentIndex())
			{
			case 0:
				get_cpu_usage(ui->spinBox_step->text().toInt(),
						ui->spinBox_totoalSteps->text().toInt(),file.toAscii().constData());
				break;
			case 1:
				get_network_usage(ui->spinBox_step->text().toInt(),
						ui->spinBox_totoalSteps->text().toInt(),file.toAscii().constData());
				break;
			case 2:
				get_memory_usage(ui->spinBox_step->text().toInt(),
						ui->spinBox_totoalSteps->text().toInt(),file.toAscii().constData());
				break;
			case 3:
				get_disk_usage(ui->spinBox_step->text().toInt(),
						ui->spinBox_totoalSteps->text().toInt(),file.toAscii().constData());
				break;
			}
			//弹出数据搜集完成的对话框
			QMessageBox::warning(this,"Success","Data Get OK : " + file);
			ui->pushButton_StartStoreData->setDisabled(false);
			//调用gedit打开生成的文件
			QString program = "gedit";
			QStringList arguments;
			arguments << file;

			QProcess *myProcess = new QProcess(this);
			myProcess->start(program, arguments);
		}
	}
}

//Select File按钮按下后的响应函数
void NewCpuTools::on_pushButton_SelectInputFile_clicked()
{
	QString file;
	QFileDialog qfd;
	//调用文件窗口获取文件名
	file = qfd.getOpenFileName(this,"Select One File",".",tr("Text files(*.txt)"));
	//如果没有选择文件，弹出警告
	if(file.isEmpty()){
		QMessageBox::warning(this,"Warning","No File Selected!");
	}
	else{
		//将文件名显示到控件中
		ui->label_GenSelectedInputFileName->setText(file);
		qDebug() <<file;
	}
}

//DIR Select按钮后的响应函数
void NewCpuTools::on_pushButton_SelectOutpuDir_clicked()
{
	QString file;
	QFileDialog qfd;
	//选择文件夹
	file = qfd.getExistingDirectory(this,"Select One Dir",".");

	//如果为空则警告
	if(file.isEmpty()){
		QMessageBox::warning(this,"Warning","No Dir Selected!");
		qDebug() << "empty";
	}
	else {
		//如果文件名为空则警告
		if(ui->lineEdit_GenInputFileName->text().isEmpty()){
			qDebug() << "empty";
			QMessageBox::warning(this,"Warning","Error! File Name is Not Seted!");
		}
		else{
			//合成输出文件名=绝对路径+文件名
			file += "/" + ui->lineEdit_GenInputFileName->text() + "." +
				ui->comboBox_GenOutputFileType->currentText();
			//禁用文件格式选择框
			ui->comboBox_GenOutputFileType->setDisabled(true);
			//将文件名显示到控件中
			ui->label_GenSelectedOutputFileName->setText(file);
			qDebug() <<file;
		}
	}
}

//Start Genernate按钮按下后相响应函数
void NewCpuTools::on_pushButton_GenStart_pressed()
{
	//如果文件名与文件夹任一个为空的，则弹出警告对话框，否则进行转换
	if(ui->lineEdit_GenInputFileName->text().isEmpty() ||
			ui->label_GenSelectedInputFileName->text().isEmpty()){
		QMessageBox::warning(this,"Warning","Error! Input or Output File Name is Error!");
	}
	else{
		//定义要执行的程序及参数
	    QString program,file;
	    QStringList arguments;
		//定义所需执行命令的容器
	    QProcess *myProcess = new QProcess(this);

		//根据当前文件格式选择控件的索引，转换成对应的文件格式
		//调用对应的库函数生成文件
		switch(ui->comboBox_GenOutputFileType->currentIndex())
		{
		case 0:
			//调用转为JPG文件的函数
			gen_jpg_file(ui->label_GenSelectedInputFileName->text().toAscii().constData()/* 将控件中的文本转为 char * */,
					ui->label_GenSelectedOutputFileName->text().toAscii().constData());
			//file =  ui->label_GenSelectedOutputFileName->text() + "_all-cpu.jpg";
			arguments << file;
			program = "nautilus";
			//program = "eog";
			break;
		case 1:
			//调用转为PDF文件的函数
			gen_pdf_file(ui->label_GenSelectedInputFileName->text().toAscii().constData(),
					ui->label_GenSelectedOutputFileName->text().toAscii().constData());
			file =  ui->label_GenSelectedOutputFileName->text();
			arguments << file;
			program = "nautilus";
			//program = "evince";
			break;
		case 2:
			gen_png_file(ui->label_GenSelectedInputFileName->text().toAscii().constData(),
					ui->label_GenSelectedOutputFileName->text().toAscii().constData());
			break;
		case 3:
			gen_csv_file(ui->label_GenSelectedInputFileName->text().toAscii().constData(),
					ui->label_GenSelectedOutputFileName->text().toAscii().constData());
			break;
		}
		//弹出转换完成的对话框
		QMessageBox::warning(this,"Success","Data convert ok : " + ui->label_GenSelectedOutputFileName->text());
		//运行对应程序打开生成的文件
		myProcess->start(program, arguments);
	}
}